import java.util.concurrent.CountDownLatch;

public class Demo10 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 5; i++) {
            int id = i;
            Thread t = new Thread(() -> {
//这里涉及变量捕获，java获取这个变量，但是final或者事实上final的变量(因为进行了++，改进）
                System.out.println("线程" + id+ "开始工作");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程" + id+ "完成工作");
                //这里输入的是id,因为输入i会报错，i是局部变量
            countDownLatch.countDown();});
              t.start();}
//countDownLatch.await()负责等待任务结束,a->all等待全部任务结束，当调用countDown次数<初始化设置的次数await就会阻塞等待
        countDownLatch.await();
        System.out.println("全部完成了");
        }
    }